/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * SetScaleDialog.java
 *
 * Created on 2011-07-11, 21:32:26
 */
package JFreeChartExtensions.ChartPanelDialogs;

import JFreeChartExtensions.ExtendableChartPanel;
import org.jfree.chart.axis.LogarithmicAxis;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.XYPlot;

/**
 *
 * @author Nobody
 */
public class SetScaleDialog extends ChartPanelDialog {

    private XYPlot xYPlot;

    public SetScaleDialog(ExtendableChartPanel chart) {
        super(chart);
        initComponents();
        registerButtonToGroup();
        xYPlot = chart.getChart().getXYPlot();
        setInitialValuesForRadioButton();

    }

    private void registerButtonToGroup() {
        XAxisButtonGroup.add(XLinearButton);
        XAxisButtonGroup.add(XLogButton);

        YAxisButtonGroup.add(YLinearButton);
        YAxisButtonGroup.add(YLogButton);

    }

    private void setInitialValuesForRadioButton() {
        if (xYPlot.getDomainAxis() instanceof LogarithmicAxis) {
            XAxisButtonGroup.setSelected(XLogButton.getModel(), true);
        } else if (xYPlot.getDomainAxis() instanceof NumberAxis) {
            XAxisButtonGroup.setSelected(XLinearButton.getModel(), true);
        }
        if (xYPlot.getRangeAxis() instanceof LogarithmicAxis) {
            YAxisButtonGroup.setSelected(YLogButton.getModel(), true);
        } else if (xYPlot.getRangeAxis() instanceof NumberAxis) {
            YAxisButtonGroup.setSelected(YLinearButton.getModel(), true);
        }
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        XAxisButtonGroup = new javax.swing.ButtonGroup();
        YAxisButtonGroup = new javax.swing.ButtonGroup();
        mainPanel = new javax.swing.JPanel();
        YAxis = new javax.swing.JPanel();
        YLinearButton = new javax.swing.JRadioButton();
        YLogButton = new javax.swing.JRadioButton();
        XAxis = new javax.swing.JPanel();
        XLinearButton = new javax.swing.JRadioButton();
        XLogButton = new javax.swing.JRadioButton();
        buttonPanel = new javax.swing.JPanel();
        OKButton = new javax.swing.JButton();
        CancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        YAxis.setBorder(javax.swing.BorderFactory.createTitledBorder("Y Axis"));

        YLinearButton.setText("Linear");

        YLogButton.setText("Logarithmic");

        javax.swing.GroupLayout YAxisLayout = new javax.swing.GroupLayout(YAxis);
        YAxis.setLayout(YAxisLayout);
        YAxisLayout.setHorizontalGroup(
            YAxisLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(YAxisLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(YAxisLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(YLinearButton)
                    .addComponent(YLogButton))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        YAxisLayout.setVerticalGroup(
            YAxisLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(YAxisLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(YLinearButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(YLogButton))
        );

        XAxis.setBorder(javax.swing.BorderFactory.createTitledBorder("X Axis"));

        XLinearButton.setText("Linear");

        XLogButton.setText("Logarithmic");

        javax.swing.GroupLayout XAxisLayout = new javax.swing.GroupLayout(XAxis);
        XAxis.setLayout(XAxisLayout);
        XAxisLayout.setHorizontalGroup(
            XAxisLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, XAxisLayout.createSequentialGroup()
                .addContainerGap(11, Short.MAX_VALUE)
                .addGroup(XAxisLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(XLinearButton)
                    .addComponent(XLogButton))
                .addContainerGap())
        );
        XAxisLayout.setVerticalGroup(
            XAxisLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(XAxisLayout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(XLinearButton)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(XLogButton))
        );

        OKButton.setText("OK");
        OKButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                OKButtonMouseClicked(evt);
            }
        });
        buttonPanel.add(OKButton);

        CancelButton.setText("Cancel");
        CancelButton.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                CancelButtonMouseClicked(evt);
            }
        });
        buttonPanel.add(CancelButton);

        javax.swing.GroupLayout mainPanelLayout = new javax.swing.GroupLayout(mainPanel);
        mainPanel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addComponent(XAxis, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(YAxis, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(buttonPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 221, Short.MAX_VALUE))
                .addContainerGap())
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(YAxis, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(XAxis, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(buttonPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(mainPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(mainPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void OKButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_OKButtonMouseClicked
       // parentPanel.setRevertAxis(false);
        if (XAxisButtonGroup.isSelected(XLogButton.getModel())) {
            for (int i = 0; i < xYPlot.getDomainAxisCount(); ++i) {
                String label = xYPlot.getDomainAxis(i).getLabel();
                LogarithmicAxis biglog = new LogarithmicAxis(label);
                biglog.setAllowNegativesFlag(true);
                parentPanel.setDomainAxisAndNotifyListeners(i, biglog);
            }
        } else {
            for (int i = 0; i < xYPlot.getDomainAxisCount(); ++i) {
                String label = xYPlot.getDomainAxis(i).getLabel();
                parentPanel.setDomainAxisAndNotifyListeners(i, new NumberAxis(label));
            }
        }
        if (YAxisButtonGroup.isSelected(YLogButton.getModel())) {
            for (int i = 0; i < xYPlot.getRangeAxisCount(); ++i) {
                String label = xYPlot.getRangeAxis(i).getLabel();
                LogarithmicAxis biglog = new LogarithmicAxis(label);
                biglog.setAllowNegativesFlag(true);
                parentPanel.setRangeAxisAndNotifyListeners(i, biglog);
            }
        } else {
            for (int i = 0; i < xYPlot.getRangeAxisCount(); ++i) {
                String label = xYPlot.getRangeAxis(i).getLabel();
                parentPanel.setRangeAxisAndNotifyListeners(i, new NumberAxis(label));
            }
        }
     //   parentPanel.setRevertAxis(true);
        dispose();
    }//GEN-LAST:event_OKButtonMouseClicked

    private void CancelButtonMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_CancelButtonMouseClicked
        dispose();
    }//GEN-LAST:event_CancelButtonMouseClicked

   /* @Override
    public void dispose() {
        if (frameWithChart != null) {
            frameWithChart.setEnabled(true);
        }
        super.dispose();
    }*/
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton CancelButton;
    private javax.swing.JButton OKButton;
    private javax.swing.JPanel XAxis;
    private javax.swing.ButtonGroup XAxisButtonGroup;
    private javax.swing.JRadioButton XLinearButton;
    private javax.swing.JRadioButton XLogButton;
    private javax.swing.JPanel YAxis;
    private javax.swing.ButtonGroup YAxisButtonGroup;
    private javax.swing.JRadioButton YLinearButton;
    private javax.swing.JRadioButton YLogButton;
    private javax.swing.JPanel buttonPanel;
    private javax.swing.JPanel mainPanel;
    // End of variables declaration//GEN-END:variables
}
